package com.kl.forum.util;

import sun.misc.BASE64Decoder;

import java.io.UnsupportedEncodingException;

public class BASE64Util {
    /**
     * 将 s 进行 BASE64 编码
     *
     * @return String
     * @author lifq
     * @date 2015-3-4 上午09:24:02
     */
    public static String encode(String s) {
        if (s == null)
            return null;
        String res = "";
        try {
            res = new sun.misc.BASE64Encoder().encode(s.getBytes("GBK"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 将 BASE64 编码的字符串 s 进行解码
     *
     * @return String
     * @author lifq
     * @date 2015-3-4 上午09:24:26
     */
    public static String decode(String s) {
        if (s == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(s);
            return new String(b,"GBK");
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String c="W3siY29sdW1uTmFtZSI6Ir2hye0iLCJpZCI6NX0seyJjb2x1bW5OYW1lIjoi1Pa8oSIsImlkIjo2\n" +
                "fSx7ImNvbHVtbk5hbWUiOiLK08a1IiwiaWQiOjF9LHsiY29sdW1uTmFtZSI6IsS406QiLCJpZCI6\n" +
                "Mn0seyJjb2x1bW5OYW1lIjoiwb3Q1CIsImlkIjozfSx7ImNvbHVtbk5hbWUiOiLDwMjdIiwiaWQi\n" +
                "OjR9XQ==";
       String a ="[{\"columnName\":\"健身\",\"id\":5},{\"columnName\":\"增肌\",\"id\":6},{\"columnName\":\"视频\",\"id\":1},{\"columnName\":\"母婴\",\"id\":2},{\"columnName\":\"两性\",\"id\":3},{\"columnName\":\"美容\",\"id\":4}]";
       String b= encode(a);
       String d=decode(c);
       System.out.println(b);
       System.out.println(d);
    }

}
